package com.wanmait.recruit.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.pojo.Position;
import com.wanmait.recruit.pojo.PositionType;
import com.wanmait.recruit.service.PositionService;
import com.wanmait.recruit.service.PositionTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Transactional
@RequestMapping("/manage/position")
public class PositionController {
    @Resource
    private PositionService positionService;
    @Resource
    private PositionTypeService positionTypeService;
    @RequestMapping("positionList")
    public ModelAndView positionList(){
        ModelAndView modelAndView = new ModelAndView();
        List<PositionType> positionTypes = positionTypeService.findAll();
        modelAndView.addObject("positionTypes",positionTypes);
        modelAndView.setViewName("/manage/position/positionList");
        return modelAndView;
    }
    @RequestMapping("positionListData")
    public ModelAndView positionListData(String name,Integer typeId,Integer pageNum){
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Position> pageInfo = positionService.findAll(name, typeId, pageNum);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("/manage/position/positionListData");
        return modelAndView;
    }
    @PostMapping("insert")
    public String insert(String name,Integer typeId,Integer heat){
        Position position = new Position();
        position.setPositionName(name);
        position.setPositionTypeId(typeId);
        position.setHeat(heat);
        positionService.insert(position);
        return "redirect:/manage/position/insert";
    }
    @RequestMapping("insert")
    public ModelAndView insert(){
        ModelAndView modelAndView = new ModelAndView();
        List<PositionType> positionTypes = positionTypeService.findAll();
        modelAndView.addObject("positionTypes",positionTypes);
        modelAndView.setViewName("/manage/position/insert");
        return modelAndView;
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        positionService.delete(id);
        return "redirect:/manage/position/positionList";
    }
    @RequestMapping("isRename")
    @ResponseBody
    public String isRename(String name){
        Boolean flag = positionService.isRename(name);
        if(flag){
            return "1";
        }else{
            return "0";
        }
    }
}
